//
//  CoreImageViewController.m
//  DealImage
//
//  Created by hs on 2017/6/30.
//  Copyright © 2017年 jiachen. All rights reserved.
//

#import "CoreImageViewController.h"
#include "cubeMap.c"

@interface CoreImageViewController ()
{
    UIImageView *_orginImageView;
   
}
@end

@implementation CoreImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self drawUI];
    // Do any additional setup after loading the view.
}

- (void)drawUI{
    UIImage *image1 = [UIImage imageNamed:@"p1.jpg"];
    _orginImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"testPic.png"]];
//    [_orginImageView sizeToFit];
    _orginImageView.frame = CGRectMake(0,64,300,300);
    [self.view addSubview:_orginImageView];
    
    UIButton *cropImageBtn = [[UIButton alloc]initWithFrame:CGRectMake(20,self.view.frame.size.height - 64,100,60)];
    [cropImageBtn setTitle:@"抠图" forState:UIControlStateNormal];
    cropImageBtn.backgroundColor = [UIColor lightGrayColor];
    [cropImageBtn addTarget:self action:@selector(cropImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cropImageBtn];
}

- (void)cropImage{
    
    UIImage *image2 = [UIImage imageNamed:@"background.png"];
    struct CubeMap myCube = createCubeMap(70, 90);
    NSData *mydata = [[NSData alloc]initWithBytesNoCopy:myCube.data length:myCube.length freeWhenDone:true];
    CIFilter *colorCubeFilter = [CIFilter filterWithName:@"CIColorCube"];
    [colorCubeFilter setValue:[NSNumber numberWithFloat:myCube.dimension] forKey:@"inputCubeDimension"];
    [colorCubeFilter setValue:mydata forKey:@"inputCubeData"];
    [colorCubeFilter setValue:[CIImage imageWithCGImage:_orginImageView.image.CGImage] forKey:kCIInputImageKey];
    
    CIImage *outputImage = colorCubeFilter.outputImage;
    CIFilter *sourceOverCompositingFilter = [CIFilter filterWithName:@"CISourceOverCompositing"];
    [sourceOverCompositingFilter setValue:outputImage forKey:kCIInputImageKey];
    [sourceOverCompositingFilter setValue:[CIImage imageWithCGImage:image2.CGImage] forKey:kCIInputBackgroundImageKey];
    outputImage = sourceOverCompositingFilter.outputImage;
    struct CGImage *cgImage = [[CIContext contextWithOptions:nil]createCGImage:outputImage fromRect:outputImage.extent];
  
    UIImageView *newImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 300, 300, 300)];
    _orginImageView.image = [UIImage imageWithCGImage:cgImage];
 //   [self.view addSubview:newImageView];
}

- (NSString *)title{
    
    return @"CoreImage";
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
